package org.zfes.snowier.cloud.zbss.asf.router;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.arcsoft.face.FaceInfo;
import com.arcsoft.face.FunctionConfiguration;
import com.arcsoft.face.enums.ImageFormat;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.zfes.snowier.web.controller.BaseController;
import org.zfes.snowier.cloud.zbss.asf.biz.dto.FaceUserResDto;
import org.zfes.snowier.cloud.zbss.asf.biz.dto.ImageInfo;
import org.zfes.snowier.cloud.zbss.asf.biz.dto.ProcessResDto;
import org.zfes.snowier.cloud.zbss.asf.biz.dto.UserFaceInfoCreateDto;
import org.zfes.snowier.cloud.zbss.asf.biz.dto.UserFaceInfoUpdateDto;
import org.zfes.snowier.cloud.zbss.asf.biz.model.UserFaceInfo;
import org.zfes.snowier.cloud.zbss.asf.biz.service.IFaceEngineService;
import org.zfes.snowier.cloud.zbss.asf.biz.service.IUserFaceInfoService;
import org.zfes.snowier.cloud.zbss.asf.biz.util.ImageUtil;
import org.zfes.snowier.common.file.FileResInfo;
import org.zfes.snowier.common.file.disk.UploadUitl;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
@Controller 
@RequestMapping("/reco/face")
public class FaceRecognitionController extends BaseController {

	
	@Autowired
	private IUserFaceInfoService userFaceInfoService;
	
	@Autowired
	private IFaceEngineService faceEngineService;
	
	
	  @RequestMapping(value="/binaryImg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public  @ResponseBody Object binaryImg(@RequestParam(name="file",required=false)MultipartFile file ) throws IOException{
		  
	  	Optional<ImageInfo> imageInfoOp=ImageUtil.getRGBData(file.getInputStream());
	  	if(!imageInfoOp.isPresent()) {
				ZAlert.throwSLE( "图像转化错误");
	  	}
	 	 List<FaceInfo> faceInfoList = faceEngineService.detectFaces(imageInfoOp.get());
   	  	 ZAssert.notEmpty(faceInfoList,"未检测到人脸");

   	    ProcessResDto dto=faceEngineService.process(imageInfoOp.get(),faceInfoList);
   	    System.out.println("ProcessResDto:"+dto);
		 byte[] feature= faceEngineService.extractFaceFeature(imageInfoOp.get(),faceInfoList);

		 List<FaceUserResDto> resList = faceEngineService.compareFaceFeature(feature);
		 System.out.println("handleTextMessage after compareFaceFeature-size:"+ resList.size());
		 
	     return ajaxDoneSuccess("数据操作成功 ").put("resList", resList);
	    }
	  
	  @RequestMapping(value="/base64Img", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	    public  @ResponseBody Object base64Img(String faceimg) throws IOException{
		  
		  Optional<ImageInfo> imageInfoOp=ImageUtil.base64StringToImage(faceimg);
			if(!imageInfoOp.isPresent()) {
				ZAlert.throwSLE( "图像转化错误");
			}
		  List<FaceInfo> faceInfoList = faceEngineService.detectFaces(imageInfoOp.get());
		  ZAssert.notEmpty(faceInfoList,"未检测到人脸");

		  faceEngineService.process(imageInfoOp.get(),faceInfoList);

		  byte[] feature= faceEngineService.extractFaceFeature(imageInfoOp.get(),faceInfoList);

		  List<FaceUserResDto> resList = faceEngineService.compareFaceFeature(feature);
		  System.out.println("handleTextMessage after compareFaceFeature-size:"+ resList.size());
		  return ajaxDoneSuccess("数据操作成功 ").put("resList", resList);
	    }
//==================================================================

	
	

	


}